package com.anos.smsfun.task;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.provider.ContactsContract.Contacts;
import android.util.Log;

import com.anos.smsfun.cache.SMSFunDataCache;
import com.anos.smsfun.controller.ThreadManager;
import com.anos.smsfun.model.MessageItem;

public class QueryUtils {

	/**
	 * insert sms/sent
	 * 
	 */
	public static void insertSmsSentItem(final Context context,
			final MessageItem obj) {
		ThreadManager.getInstance(context).execute(new Runnable() {

			@Override
			public void run() {
				try {
					final String SMS_URI_SENT = "content://sms/sent";

					ContentValues values = new ContentValues();
					values.put("address", obj.getAddress());
					values.put("body", obj.getBody());
					values.put("date", obj.getLongDate());
					values.put("type", obj.getType());
					values.put("status", obj.getStatus());
					values.put("seen", obj.getSeen());

					Uri uri = Uri.parse(SMS_URI_SENT);
					context.getContentResolver().insert(uri, values);

				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
	}

	/**
	 * Update sms/sent
	 * 
	 * Tinh trang da gui hay chua
	 * 
	 */
	public static boolean updateSmsSentItem(Context context, MessageItem obj) {
		boolean ret = false;
		try {
			final String SMS_URI_SENT = "content://sms/sent";

			ContentValues values = new ContentValues();
			values.put("date", obj.getLongDate());
			values.put("status", obj.getStatus());

			Uri uri = Uri.parse(SMS_URI_SENT);
			context.getContentResolver().update(uri, values,
					"_id=" + obj.getId(), null);

			ret = true;
		} catch (Exception ex) {
			ret = false;
		}
		return ret;
	}

	/**
	 * Update sms/sent
	 * 
	 * Tinh trang da doc tin nhan
	 * 
	 */
	public static boolean updateSmsSeen(Context context, MessageItem obj) {
		boolean ret = false;
		try {
			final String SMS_URI_SENT = "content://sms/";

			ContentValues values = new ContentValues();
			values.put("date", obj.getLongDate());
			values.put("seen", obj.getSeen());

			Uri uri = Uri.parse(SMS_URI_SENT);
			context.getContentResolver().update(uri, values,
					"_id=" + obj.getId(), null);

			ret = true;
		} catch (Exception ex) {
			ret = false;
		}
		return ret;
	}

	/**
	 * delete message
	 * 
	 */
	public static int deleteMessage(Context context, int id) {
		try {
			int count = 0;
			// Delete the SMS
			String pid = String.valueOf(id);
			String uri = "content://sms/" + pid;
			count = context.getContentResolver().delete(Uri.parse(uri), null,
					null);
			return count;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * delete conversation
	 * 
	 */
	public static int deleteConvsersation(Context context, long thread_id) {
		try {
			int count = 0;
			// Delete the SMS
			String pid = String.valueOf(thread_id);
			String uri = "content://sms/conversations/" + pid;
			count = context.getContentResolver().delete(Uri.parse(uri), null,
					null);
			return count;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * xóa danh bạ trên bộ nhớ cache, sau đó xóa trên Contacts.CONTENT_URI
	 * 
	 */
	public static int deleteContact(Context context, long contact_id) {
		try {
			// xóa contact trên bộ nhớ cache
			SMSFunDataCache mCache = SMSFunDataCache.getInstance(context);
			mCache.removeContact(contact_id);

			// xóa contact trên Contacts.CONTENT_URI
			String uri = Contacts.CONTENT_URI + "/"
					+ String.valueOf(contact_id);
			context.getContentResolver().delete(Uri.parse(uri), null, null);
			Log.e("DELETE", "deleted");
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

}
